package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import ConfigFile.Loads;
import ConfigFile.Score;
import ConfigFile.ScoreFile;

public class MarkTable extends JFrame implements ActionListener {
	private JButton btSave;
	private JTable jTable1;
	private JPanel pnl;
	private String[][] data;
	private int score;
	private List<Score> list = new ArrayList<Score>();

	public MarkTable(int score) {
		this.pnl = new JPanel(new BorderLayout());
		this.score = score;
		data = new String[10][2];

		Object[] header = { "Name", "Score" };
		jTable1 = new JTable(data, header);
		loadHightScore();
		jTable1.setEnabled(true);

		btSave = new JButton("save");
		 btSave.setBackground(Color.white);
		btSave.addActionListener(this);

		this.pnl.add(new JScrollPane(jTable1), BorderLayout.CENTER);
		JPanel p = new JPanel(new GridLayout(2, 2));

		JLabel lable_name = new JLabel("Name");
		lable_name.setBackground(Color.white);
		lable_name.setOpaque(true);
		JTextField text_name = new JTextField();

		JLabel lable_score = new JLabel("Score");
		lable_score.setBackground(Color.white);
		lable_score.setOpaque(true);
		JTextField text_score = new JTextField();

		p.add(lable_name);
		p.add(text_name);
		p.add(lable_score);
		p.add(text_score);
//		p.add(btSave);
		this.add(pnl);
		this.add(p);
		this.add(btSave, BorderLayout.SOUTH);
		setLayout(new GridLayout(0, 1));
		this.setVisible(true);
		this.setAlwaysOnTop(true);
		this.setBounds(300, 100, 300, 400);

	}

	private void loadHightScore() {
		ScoreFile scorefile = Loads.loadScoreFile();
		if (scorefile != null) {
			int size = scorefile.size();
			list = scorefile.getListScore();
			Score scores = null;
			for (int i = 0; i < size; i++) {
				scores = list.get(i);
				jTable1.setValueAt(scores.getName(), i, 0);
				jTable1.setValueAt(scores.getScore(), i, 1);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {

	}

	public static void main(String[] args) {
		MarkTable m = new MarkTable(10);
	}

}
